#pragma once

#include <cstdint>

namespace limbo::order {

#ifndef htobe16

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htobe16(x) __builtin_bswap16(x)
#define htole16(x) __uint16_identity(x)
#define be16toh(x) __builtin_bswap16(x)
#define le16toh(x) __uint16_identity(x)

#define htobe32(x) __builtin_bswap32(x)
#define htole32(x) __uint32_identity(x)
#define be32toh(x) __builtin_bswap32(x)
#define le32toh(x) __uint32_identity(x)

#else
#define htobe16(x) __uint16_identity(x)
#define htole16(x) __bswap_16(x)
#define be16toh(x) __uint16_identity(x)
#define le16toh(x) __bswap_16(x)

#define htobe32(x) __uint32_identity(x)
#define htole32(x) __bswap_32(x)
#define be32toh(x) __uint32_identity(x)
#define le32toh(x) __bswap_32(x)
#endif

#endif

inline uint16_t native_to_big(uint16_t value) { return htobe16(value); }

inline uint16_t big_to_native(uint16_t value) { return be16toh(value); }

inline uint32_t native_to_big(uint32_t value) { return htobe32(value); }

inline uint32_t big_to_native(uint32_t value) { return be32toh(value); }

} // namespace limbo::order
